﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Big_Data.View
{
    public partial class FrmStation : Form
    {
        DataBase userDB = new DataBase();

        public FrmStation()
        {
            InitializeComponent();
        }

        private void dgwStationType_SelectionChanged(object sender, EventArgs e)
        {
            //判断是否选择了行数据，如果选择项为0，则什么都不处理。
            if (dgwStationType.SelectedRows.Count == 0)
            {
                return;
            }

            //将数据绑定到左侧文本框中
            DataGridViewRow currentRow = dgwStationType.CurrentRow;
            if (currentRow.Cells[0].Value != null)
            {
                this.txtSta_code.Text = currentRow.Cells[0].Value.ToString();
                this.txtSta_name.Text = currentRow.Cells[1].Value.ToString();
                this.txtSta_address.Text = currentRow.Cells[2].Value.ToString();
                this.cbbdis.SelectedValue = currentRow.Cells[3].Value.ToString();
                this.cbbstationtype.SelectedValue = currentRow.Cells[4].Value.ToString();
                this.cbbarea.SelectedValue = currentRow.Cells[5].Value.ToString();
            }
        }

        private void FrmStation_Load(object sender, EventArgs e)
        {
            InitDataBind();
            //初始化绑定数据
            bindStationDate();
        }


        private void InitDataBind()
        {
            //加油站表
            string S_station_sql = "select * from S_stationtype";
            DataTable dt_S_station = userDB.show(S_station_sql);
            this.cbbstationtype.ValueMember = "Sta_usercode";
            this.cbbstationtype.DisplayMember = "Sta_username";
            this.cbbstationtype.DataSource = dt_S_station;

            //工程师表
            string S_engineer_sql = "select * from S_area";
            DataTable dt_S_area = userDB.show(S_engineer_sql);
            this.cbbarea.ValueMember = "Area_code";
            this.cbbarea.DisplayMember = "Area_name";
            this.cbbarea.DataSource = dt_S_area;

            //分销商（服务商）表
            string S_distributor_sql = "select * from S_distributor";
            DataTable dt_S_distributor = userDB.show(S_distributor_sql);
            this.cbbdis.ValueMember = "Dis_code";
            this.cbbdis.DisplayMember = "Dis_name";
            this.cbbdis.DataSource = dt_S_distributor;

        }
        private void bindStationDate()
        {
            try
            {
                string sqlstr = "select * from S_station";
                DataTable table = userDB.show(sqlstr);
                this.dgwStationType.DataSource = table;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                userDB.conn_close();
            }
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //查询code是否存在
                string sqlExists = string.Format("select * from S_station where  Sta_usercode='{0}'", this.txtSta_code.Text);
                DataSet ds = userDB.getDs(sqlExists, "S_station");

                var _Sta_code       = this.txtSta_code.Text;
                var _Sta_name       = this.txtSta_name.Text;
                var _Sta_address    = this.txtSta_address.Text;
                var _Dis_code       = this.cbbdis.SelectedValue.ToString();
                var _Sta_usercode   = this.cbbstationtype.SelectedValue.ToString();
                var _Area_code      = this.cbbarea.SelectedValue.ToString();

                //如果能查询到数据，则为编辑，否则新增
                if (ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string sqlstr = string.Format("update S_station set Sta_name='{1}',Sta_address='{2}',Dis_code='{3}',Sta_usercode='{4}',Area_code='{5}' where  Sta_code='{0}'",
                        _Sta_code,
                        _Sta_name,
                        _Sta_address,
                        _Dis_code,
                        _Sta_usercode,
                        _Area_code);
                    userDB.Distributortableinsert(sqlstr);
                    MessageBox.Show("修改成功！");
                }
                else
                {
                    string sqlstr = string.Format("insert into S_station( Sta_code,Sta_name,Sta_address,Dis_code,Sta_usercode,Area_code) values('{0}','{1}','{2}','{3}','{4}','{5}')",
                          _Sta_code,
                        _Sta_name,
                        _Sta_address,
                        _Dis_code,
                        _Sta_usercode,
                        _Area_code);
                    userDB.Distributortableinsert(sqlstr);
                    MessageBox.Show("新增成功！");
                }

                //编辑完毕，重新绑定刷新数据
                bindStationDate();
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据插入错误" + ex.Message);
            }
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            DialogResult dialogClose = MessageBox.Show("你确定要删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dialogClose == DialogResult.OK)
            {
                try
                {
                    string Sta_code = txtSta_code.Text.Trim();
                    string delete = "DELETE FROM S_station WHERE Sta_code ='" + Sta_code + "'";
                    userDB.Distributortabledelete(delete);

                    //编辑完毕，重新绑定刷新数据
                    bindStationDate();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("数据删除错误" + ex.Message);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.txtSta_code.Text ="";
            this.txtSta_name.Text = "";
            this.txtSta_address.Text = "";
            this.cbbdis.SelectedIndex = -1;
            this.cbbstationtype.SelectedValue = -1;
            this.cbbarea.SelectedValue = -1;
        }
    }
}
